package com.czy.owner.ui.workorder;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.db.UserHelper;
import com.czy.owner.global.Constants;
import com.czy.owner.rxbus.RxBus;
import com.czy.owner.rxbus.event.CommonEvent;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wangyang on 2016/9/18 0018.
 * 订单评价
 */
public class EvaluateActivity2 extends BaseActivity {
    @BindView(R.id.ratingbar_big_encourage)
    RatingBar ratingbarBigEncourage;
    @BindView(R.id.ratingbar_intimate_index)
    RatingBar ratingbarIntimateIndex;
    @BindView(R.id.ratingbar_benefit_index)
    RatingBar ratingbarBenefitIndex;
    @BindView(R.id.ratingbar_efficiency_index)
    RatingBar ratingbarEfficiencyIndex;
    @BindView(R.id.et_evaluate_content)
    EditText etEvaluateContent;
    @BindView(R.id.tv_str_length)
    TextView tvStrLength;
    private String session = "";
    private String orderNumber;
    private int storeId;

    private boolean isMessage;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_evaluate;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.order_evaluate);
        storeId = getIntent().getIntExtra("storeId", 0);
        isMessage = getIntent().getBooleanExtra("isMessage", false);

        session = UserHelper.getInstance().getSessionInfoModel(EvaluateActivity2.this).getSession();
        orderNumber = getIntent().getStringExtra("orderNumber");
        etEvaluateContent.addTextChangedListener(new EvaluateActivity2.NewWatcher());
    }

    @Override
    protected void updateViews() {

    }


    //    验证输入框
    public class NewWatcher implements TextWatcher {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            tvStrLength.setText("剩余" + (200 - etEvaluateContent.getText().toString().length()) + "个字符");
        }

    }

    @OnClick(R.id.btn_commit)
    public void CommitOnClick(View view) {
        if (ratingbarBigEncourage.getRating() == 0.0) {
            PhoneUtils.ShowToastMessage(EvaluateActivity2.this, "鼓励指数不能为空");
            return;
        }

        if (ratingbarIntimateIndex.getRating() == 0.0) {
            PhoneUtils.ShowToastMessage(EvaluateActivity2.this, "贴心指数不能为空");
            return;
        }

        if (ratingbarBenefitIndex.getRating() == 0.0) {
            PhoneUtils.ShowToastMessage(EvaluateActivity2.this, "实惠指数不能为空");
            return;
        }

        if (ratingbarEfficiencyIndex.getRating() == 0.0) {
            PhoneUtils.ShowToastMessage(EvaluateActivity2.this, "效率指数不能为空");
            return;
        }

        String evaluateContent = etEvaluateContent.getText().toString().trim();
//        if (TextUtils.isEmpty(evaluateContent)) {
//            PhoneUtils.ShowToastMessage(EvaluateActivity2.this, "请给出评价内容");
//            etEvaluateContent.requestFocus();
//            return;
//        }
        commitEvaluate(evaluateContent);

    }

    private void commitEvaluate(String content) {
        MyLog.e("hep", storeId + "-----------------");
        mLoadView.ShowLoadView();
        String url = Constants.APP_API_BASE + Constants.OWN_ORDER_ORDERCOMMENT;
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("session", session);
        params.addBodyParameter("storeId", storeId + "");
        params.addBodyParameter("content", content);
        params.addBodyParameter("orderNumber", orderNumber);
        params.addBodyParameter("satisfaction", (int) (ratingbarBigEncourage.getRating()) + "");
        params.addBodyParameter("sweetIndex", (int) (ratingbarIntimateIndex.getRating()) + "");
        params.addBodyParameter("economicalIndex", (int) (ratingbarBenefitIndex.getRating()) + "");
        params.addBodyParameter("efficiencyIndex", (int) (ratingbarEfficiencyIndex.getRating()) + "");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    String data = PhoneUtils.checkResponseFlag(EvaluateActivity2.this, result);
                    if (data != null) {
                        PhoneUtils.ShowToastMessage(EvaluateActivity2.this, "评价成功");
                        if (isMessage) {
                            startActivity(new Intent(EvaluateActivity2.this, SubscribeOrdelActivity2.class).putExtra("currentId", 0));

                        } else {
                            Intent intent = new Intent();
                            intent.setAction(SubscribeOrdelActivity.orderAction);
                            if (SubscribeOrdelActivity.currentId() == 0) {
                                intent.putExtra("currentId", 0);
                            } else if (SubscribeOrdelActivity.currentId() == 4) {
                                intent.putExtra("currentId", 0);
                            }
                            sendBroadcast(intent);
                        }
                        RxBus.getDefault().post(new CommonEvent("评价成功"));
                        finish();
                    }
                } catch (Exception e) {

                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                mLoadView.CancleLoadView();
            }
        });


    }


}
